home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / ENVIASK.BAT < prev    next >
DOS Batch File  |  1994-10-05  |  11KB  |  312 lines

  1. @echo off
  2. REM ***********************************************************
  3. REM *** EnviAsk.bat - Prompt user for value for to set into ***
  4. REM *** ver.2         an environment variable.              ***
  5. REM ***********************************************************
  6.  
  7. REM ********************************************************************
  8. REM *** Build ALL_EASK_PARMS environment variable to all input parms ***
  9. REM ********************************************************************
  10.  
  11. SET EASK_PARMS=%0.bat
  12. :NEXT_ARG
  13.    IF q%1q == qq GOTO NO_MORE_ARGS
  14.    SET EASK_PARMS=%EASK_PARMS% %1
  15.    SHIFT
  16.    GOTO NEXT_ARG
  17. :NO_MORE_ARGS
  18.  
  19. CEnviD %EASK_PARMS%
  20. SET EASK_PARMS=
  21. GOTO CENVI_EXIT
  22.  
  23. #include <OptParms.lib>
  24.  
  25. #define  NO_TIMEOUT  -100
  26.  
  27. // define MASK characters for characters in user input
  28. #define  UPPER_CASE_CHAR      'A'
  29. #define  LOWER_CASE_CHAR      'a'
  30. #define  DIGIT                '#'
  31. #define  CHAR_OR_DIGIT_UPPER  'X'
  32. #define  CHAR_OR_DIGIT_LOWER  'x'
  33. #define  ANYTHING             '?'
  34. #define  ANYTHING_UPPER       'U'
  35. #define  ANYTHING_LOWER       'l'
  36.  
  37. IntegerCmd = "INT";
  38. EscapeCmd = "ESC";
  39. TimeoutCmd = "TIME";
  40. PromptCmd = "PROMPT";
  41. MaskCmd = "MASK";
  42. HideCmd = "HIDE";
  43.  
  44. main(argc,argv)
  45. {
  46.    success = False;
  47.    undefine(EASK_PARMS);
  48.  
  49.    if ( argc < 3 ) {
  50.       ShowInstructions();
  51.       success = True;
  52.    } else {
  53.       // get the command line switches
  54.       if ( !OptionalParameter(argc,argv,MaskCmd,Mask) )
  55.          Mask = "";
  56.       if ( !OptionalParameter(argc,argv,PromptCmd,Prompt) )
  57.          Prompt = NULL;
  58.       Timeout = OptionalParameter(argc,argv,TimeoutCmd,Temp) ? atoi(Temp) : NO_TIMEOUT;
  59.       AllowEsc = OptionalParameter(argc,argv,EscapeCmd);
  60.       IntegerOnly = OptionalParameter(argc,argv,IntegerCmd);
  61.       HideChars = OptionalParameter(argc,argv,HideCmd);
  62.       if ( argc != 4 ) {
  63.          printf("Invalid command arguments. Enter /? for help.");
  64.       } else {
  65.          EnvironmentVar = argv[1];
  66.          Min = atoi(argv[2]);
  67.          Max = atoi(argv[3]);
  68.          if ( Max < Min ) {
  69.             printf("Invalid to have <Min> greater than <Max>");
  70.          } else {
  71.             if ( Input = Ask(getenv(EnvironmentVar),Min,Max,IntegerOnly,HideChars,
  72.                              Mask,Prompt,Timeout,AllowEsc) ) {
  73.                putenv(EnvironmentVar,Input);
  74.                success = True;
  75.             } /* endif */
  76.          } /* endif */
  77.       } /* endif */
  78.    } /* endif */
  79.    if ( !success )
  80.       printf("\a");
  81.    printf("\n");
  82.    return( success ? EXIT_SUCCESS : EXIT_FAILURE );
  83. }
  84.  
  85.    void
  86. ShowInstructions(void)
  87. {
  88.    printf("\n");
  89.    printf("EnviAsk: Ask user to input a value for an environment variable.\n");
  90.    printf("\n");
  91.    printf("SYNTAX: ENVIASK <EVar> <Min> <Max> [Options]\n");
  92.    printf("Where:\n");
  93.    printf("   EVar    - The Environment variable to set when selection is input, and the\n");
  94.    printf("             initial selection if this variable had a value.\n");
  95.    printf("   <Min>   - Minimum length of input string. If /%s option then this is the\n",IntegerCmd);
  96.    printf("             minimum allowable value for the integer.\n");
  97.    printf("   <Max>   - Maximum length of input string. If /%s option then this is the\n",IntegerCmd);
  98.    printf("             maximum allowable value for the integer.\n");
  99.    printf(" Options:\n");
  100.    printf("   /%s    - An integer value must be input. <Min> and <Max> apply to the value\n",IntegerCmd);
  101.    printf("             of the integer instead of the string length. Assume /%s %c%c%c%c...\n",MaskCmd,DIGIT,DIGIT,DIGIT,DIGIT);
  102.    printf("   /%s str - Prompt <str> for keyboard entry; else there is no prompt.\n",PromptCmd);
  103.    printf("   /%s    - If ESCAPE key is pressed, then exit with ErrorLevel; if no /%s\n",EscapeCmd,EscapeCmd);
  104.    printf("             then a value MUST be input.\n");
  105.    printf("   /%s - Hide input characters and display on *\n",HideCmd);
  106.    printf("   /%s sec - <sec> seconds timeout for default selection if no user input.\n",TimeoutCmd);
  107.    printf("   /%s str - <str> is a mask for validating the type of each character; <str>\n",MaskCmd);
  108.    printf("             is of the form \"%c%c%c%c%c%c%c%c\", where character meanings are:\n",
  109.                            UPPER_CASE_CHAR,LOWER_CASE_CHAR,DIGIT,CHAR_OR_DIGIT_UPPER,
  110.                            CHAR_OR_DIGIT_LOWER,ANYTHING,ANYTHING_UPPER,ANYTHING_LOWER);
  111.    printf("    %c - Characters 'A' to 'Z', converted to upper-case.\n",UPPER_CASE_CHAR);
  112.    printf("    %c - Characters 'a' to 'z', converted to lower-case.\n",LOWER_CASE_CHAR);
  113.    printf("    %c - Only the digits '0' to '9' are accepted.\n",DIGIT);
  114.    printf("    %c - Digits '0' to '9', and characters 'A' to 'Z' (converted to upper-case).\n",CHAR_OR_DIGIT_UPPER);
  115.    printf("    %c - Digits '0' to '9', and characters 'a' to 'z' (converted to lower-case).\n",CHAR_OR_DIGIT_LOWER);
  116.    printf("    %c - Any character is acceptable.\n",ANYTHING);
  117.    printf("    %c - Any character is acceptable; convert 'A' to 'Z' to upper case.\n",ANYTHING_UPPER);
  118.    printf("    %c - Any character is acceptable; convert 'A' to 'Z' to lower case.\n",ANYTHING_LOWER);
  119. }
  120.  
  121. Ask(InitialChoice,Min,Max,IntegerOnly,Hide,Mask,Prompt,Timeout,AllowEscape)
  122. {  // return entered string, else NULL
  123.    success = True;
  124.    IsAnybodyThere = False;
  125.  
  126.    BigBuffer = InitAsk(Prompt,InitialChoice,Hide,row,colStart);
  127.    PrevLen = strlen(BigBuffer);
  128.  
  129.    StartTime = time();
  130.  
  131.    for ( TotalSeconds = 0, Finished = False; !Finished; ) {
  132.  
  133.       // check if time has elapsed without any input
  134.       if ( Timeout != NO_TIMEOUT  && !IsAnybodyThere ) {
  135.          if ( Timeout < difftime(time(),StartTime) )
  136.             break;
  137.       } /* endif */
  138.  
  139.       if ( GetKey(Key) ) {
  140.          IsAnybodyThere = True;
  141.          switch ( Key.Scan ) {
  142.             #define ENTER_SCAN   0x1c
  143.             case ENTER_SCAN:
  144.                if ( IntegerOnly ) {
  145.                   if ( BigBuffer[0] != 0
  146.                   && Min <= atoi(BigBuffer) && atoi(BigBuffer) <= Max )
  147.                      Finished = True;
  148.                } else {
  149.                   if ( Min <= strlen(BigBuffer) && strlen(BigBuffer) <= Max )
  150.                      Finished = True;
  151.                } /* endif */
  152.                break;
  153.             #define UP_SCAN   0x48
  154.             case UP_SCAN:
  155.                if ( IntegerOnly && atoi(BigBuffer) < Max )
  156.                   sprintf(BigBuffer,"%d",atoi(BigBuffer)+1);
  157.                break;
  158.             #define DOWN_SCAN 0x50
  159.             case DOWN_SCAN:
  160.                if ( IntegerOnly && Min < atoi(BigBuffer) )
  161.                   sprintf(BigBuffer,"%d",atoi(BigBuffer)-1);
  162.                break;
  163.             #define LEFT_SCAN       0x4b
  164.             #define BACKSPACE_SCAN  0x0e
  165.             #define DEL_SCAN        0x53
  166.             case LEFT_SCAN:
  167.             case BACKSPACE_SCAN:
  168.             case DEL_SCAN:
  169.                if ( BigBuffer[0] != '\0' )
  170.                   BigBuffer[strlen(BigBuffer)-1] = '\0';
  171.                break;
  172.             #define ESCAPE_SCAN  0x01
  173.             case ESCAPE_SCAN:
  174.                if ( AllowEscape ) {
  175.                   success = False;
  176.                   Finished = True;
  177.                } else
  178.                   BigBuffer[0] = 0;
  179.                break;
  180.             default:
  181.                if ( Key.Char && (strlen(BigBuffer) < Max) ) {
  182.                   MaybeAddNewCharacter(BigBuffer,Key.Char,IntegerOnly,Max,Mask);
  183.                } /* endif */
  184.                break;
  185.          } /* endswitch */
  186.          if ( strlen(BigBuffer) < PrevLen ) {
  187.             // clear all positions from strlen(BigBuffer) to PrevLen
  188.             ScreenCursor(colStart + PrevLen - 1,row);
  189.             for ( i = strlen(BigBuffer); i < PrevLen; i++ ) {
  190.                printf(" ");
  191.             } /* endfor */
  192.          } /* endif */
  193.          // some key, any key, was entered so print the entire string
  194.          ScreenCursor(colStart,row);
  195.          if ( Hide ) {
  196.             for ( ast = strlen(BigBuffer); ast--; )
  197.                putchar('*');
  198.          } else {
  199.             printf("%s",BigBuffer);
  200.          }
  201.          PrevLen = strlen(BigBuffer);
  202.       } /* endif */
  203.  
  204.    } /* endfor */
  205.  
  206.    return( success ? BigBuffer : NULL );
  207. }
  208.  
  209. MaybeAddNewCharacter(BigBuffer,c,IntegerOnly,MaxInteger,Mask)
  210. {
  211.    buflen = strlen(BigBuffer);
  212.  
  213.    // Figure MaskChar to compare new value against
  214.    if ( IntegerOnly ) {
  215.       MaskChar = DIGIT;
  216.    } else if ( Mask == NULL  ||  strlen(Mask) <= buflen ) {
  217.       MaskChar = ANYTHING;
  218.    } else {
  219.       MaskChar = Mask[buflen];
  220.    } /* endif */
  221.  
  222.    switch ( MaskChar ) {
  223.    case UPPER_CASE_CHAR:
  224.       valid = ( 'A' <= (c=toupper(c))  &&  c <= 'Z' );
  225.       break;
  226.    case LOWER_CASE_CHAR:
  227.       valid = ( 'a' <= (c=tolower(c))  &&  c <= 'z' );
  228.       break;
  229.    case DIGIT:
  230.       valid = ( '0' <= c  &&  c <= '9' );
  231.       break;
  232.    case CHAR_OR_DIGIT_UPPER:
  233.       valid = ( ('A' <= (c=toupper(c))  &&  c <= 'Z')
  234.              || ('0' <= c  &&  c <= '9') );
  235.       break;
  236.    case CHAR_OR_DIGIT_LOWER:
  237.       valid = ( ('a' <= (c=tolower(c))  &&  c <= 'z')
  238.              || ('0' <= c  &&  c <= '9') );
  239.       break;
  240.    default:
  241.       printf("\nInvalid Mask Character %c\n",MaskChar);
  242.    case ANYTHING:
  243.       valid = True;
  244.       break;
  245.    case ANYTHING_UPPER:
  246.       c = toupper(c);
  247.       valid = True;
  248.       break;
  249.    case ANYTHING_LOWER:
  250.       c = tolower(c);
  251.       valid = True;
  252.       break;
  253.    } /* endswitch */
  254.  
  255.    if ( IntegerOnly && valid) {
  256.       // may become invalid if this new value puts it out of range
  257.       if ( MaxInteger < ((atoi(BigBuffer) * 10) + (c - '0')) ) {
  258.          valid = False;
  259.       } /* endif */
  260.    } /* endif */
  261.  
  262.    if ( valid ) {
  263.       BigBuffer[buflen+1] = 0;
  264.       BigBuffer[buflen] = c;
  265.    } /* endif */
  266. }
  267.  
  268. InitAsk(Prompt,InitialChoice,Hide,row,colStart)
  269. {
  270.    // If there is a prompt then write it now
  271.    if ( Prompt != NULL )
  272.       printf("%s ",Prompt);
  273.  
  274.    // get current row and column of screen so know where to write to
  275.    row = ScreenCursor().row;
  276.    colStart = ScreenCursor().col;
  277.  
  278.    // Set Buf to InitialChoice
  279.    strcpy(BigBuffer,(InitialChoice==NULL) ? "" : InitialChoice);
  280.    if ( Hide ) {
  281.       for ( ast = strlen(BigBuffer); ast--; )
  282.          putchar('*');
  283.    } else {
  284.       printf("%s",BigBuffer);
  285.    }
  286.    return BigBuffer;
  287. }
  288.  
  289.  
  290. GetKey(pKey)
  291.    // get key if one is avialable at keyboard; if no key then return False, else
  292.    // return TRUE and set following fields: Scan, Char, Key (=scan and ascii)
  293. {
  294.    // see if a key is available
  295.    reg.ah = 1;
  296.    interrupt(0x16,reg);
  297.    #define ZERO_FLAG 0x40
  298.    if ( reg.flags & ZERO_FLAG )
  299.       return False;
  300.  
  301.    // Get key from keyboard
  302.    undefine(reg);
  303.    reg.ah = 0;
  304.    interrupt(0x16,reg);
  305.    pKey.Key = reg.ax;
  306.    pKey.Scan = reg.ah;
  307.    pKey.Char = reg.al;
  308.    return True;
  309. }
  310.  
  311. :CENVI_EXIT
  312.